package project.web.api.controller;


import java.io.File;
import java.io.FileInputStream;
import java.util.Map;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import kernel.exception.BusinessException;
import kernel.web.ResultObject;
import org.apache.commons.compress.utils.FileNameUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import project.syspara.SysparaService;
import project.web.api.service.LocalSysparaService;

import org.springframework.beans.factory.annotation.Autowired;
import util.AliOssUtil;

import static com.alibaba.druid.sql.ast.SQLPartitionValue.Operator.List;

@RestController
@CrossOrigin
public class SysparaController {
	@Value("${upload.dir}")
	private  String filepath;

	@Autowired
	private LocalSysparaService localSysparaService;

	@Autowired
	private SysparaService sysparaService;
	/**
	 * 可逗号相隔，查询多个参数值。 exchange_rate_out 兑出货币和汇率; exchange_rate_in
	 * 兑入货币和汇率;withdraw_fee 提现手续费，type=fixed是单笔固定金额，=rate是百分比，结果到小数点2位。
	 * index_top_symbols 首页显示的4个品种。customer_service_url 在线客服URL
	 */

	@RequestMapping("api/syspara!getSyspara.action")
	public Object getSyspara(HttpServletRequest request) {
		ResultObject resultObject = new ResultObject();
		try {
			String code = request.getParameter("code" );
			Map<String, Object> data = localSysparaService.find(code);
			String seller_bond_price = sysparaService.find("seller_bond_price").getValue();
			boolean isMerchantDeposit = sysparaService.find("is_merchant_deposit").getBoolean();
			boolean isStartSmsCheck = sysparaService.find("is_start_sms_check").getBoolean();
			boolean isStartEmailCheck = sysparaService.find("is_start_email_check").getBoolean();
			data.put("seller_bond_price", seller_bond_price);
			data.put("is_merchant_deposit", isMerchantDeposit);
			data.put("is_start_sms_check", isStartSmsCheck);
			data.put("is_start_email_check", isStartEmailCheck);
			resultObject.setData(data);
		} catch (BusinessException e) {
			resultObject.setCode("402");
			resultObject.setMsg(e.getMessage());

		} catch (Throwable e) {
			resultObject.setCode("500");
			resultObject.setMsg("服务器错误");
		}
		return resultObject;
	}

	/**
	 * 底部外面
	 * @param request
	 * @return
	 */

	@RequestMapping("api/syspara!footercontent.action")
	public Object footercontent(HttpServletRequest request) {
		ResultObject resultObject = new ResultObject();
		String lang = request.getParameter("lang");
		try {

			List<Map<String,Object>> data = localSysparaService.footercontent(lang);
			resultObject.setData(data);
		} catch (BusinessException e) {
			resultObject.setCode("402");
			resultObject.setMsg(e.getMessage());

		} catch (Throwable e) {
			resultObject.setCode("500");
			resultObject.setMsg("服务器错误");
		}
		return resultObject;
	}

	@RequestMapping("api/syspara!footerimages.action")
	public Object footerimages(HttpServletRequest request) {
		ResultObject resultObject = new ResultObject();
		String lang = request.getParameter("lang");
		String type = request.getParameter("type");
		try {

			List<Map<String,Object>> data = localSysparaService.footerimages(lang,type);
			resultObject.setData(data);
		} catch (BusinessException e) {
			resultObject.setCode("402");
			resultObject.setMsg(e.getMessage());

		} catch (Throwable e) {
			resultObject.setCode("500");
			resultObject.setMsg("服务器错误");
		}
		return resultObject;
	}


	@GetMapping("/showimg!execute.action/{filename}")
	public void showImage(HttpServletRequest request, HttpServletResponse response,@PathVariable String filename) {
		ServletOutputStream out = null;
		FileInputStream ips = null;
		String upload = null;

		// 获取图片存放路径
		String imgPath = AliOssUtil.getPath(filepath,filename);

		try {
			ips = new FileInputStream(new File(imgPath));
			String type = FileNameUtils.getExtension(filename);

			if (type.equals("png")) {
				response.setContentType("image/png");
			} else if (type.equals("jpeg") || type.equals("jpg")) {
				response.setContentType("image/jpeg");
			}
			out = response.getOutputStream();
			int len;
			byte[] buffer = new byte[1024 * 10];
			while ((len = ips.read(buffer)) != -1) {
				out.write(buffer, 0, len);
			}
			out.flush();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (out != null) {
					out.close();
				}
				if (ips != null) {
					ips.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
